package com.win.controller;

import com.win.entities.Pay;
import com.win.entities.PayDTO;
import com.win.resp.ResultData;
import com.win.resp.ReturnCodeEnum;
import com.win.service.PayService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.concurrent.TimeUnit;

@RestController
@Tag(name = "支付微服务模块",description = "支付CRUD")
@Slf4j
public class PayController {
    @Resource
    PayService payService;
    @PostMapping(value = "/pay/add")
    @Operation(summary = "新增",description = "新增支付流水方法,json串做参数")
    public ResultData<Integer> addPay(@RequestBody Pay pay){
        System.out.println(pay.toString());
        int i = payService.addPay(pay);
        return ResultData.success(i);
    }
    @DeleteMapping(value = "/pay/del/{id}")
    @Operation(summary = "删除",description = "删除支付流水方法")
    public ResultData<Integer> deletePay(@PathVariable("id") Integer id) {
        int i = payService.deletePay(id);
        return ResultData.success(i);
    }
    @PutMapping(value = "/pay/update")
    @Operation(summary = "修改",description = "修改支付流水方法")
    public ResultData<Integer> updatePay(@RequestBody PayDTO payDTO){
        Pay pay = new Pay();
        BeanUtils.copyProperties(payDTO, pay);
        int i = payService.updatePay(pay);
        return ResultData.success(i);
    }
    @GetMapping(value = "/pay/get/{id}")
    @Operation(summary = "按照ID查流水",description = "查询支付流水方法")
    public ResultData<Pay> getById(@PathVariable("id") Integer id, HttpServletRequest request){
        String header = request.getHeader("X-Token");
        System.out.println(header);
        if (id == -1) throw new RuntimeException("id不能为负数");
        try {
            TimeUnit.SECONDS.sleep(62);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        Pay payId = payService.getPayId(id);
        return ResultData.success(payId);
    }

    @GetMapping(value = "/pay/get")
    @Operation(summary = "查找全部流水",description = "查询支付流水方法")
    public ResultData<List<Pay>> getById() {
        List<Pay> payAll;
        try {
            payAll = payService.getPayAll();
            int i = 10 / 0;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultData.fail(ReturnCodeEnum.RC500.getCode(), e.getMessage());
        }
        return ResultData.success(payAll);
    }


    @Value("${server.port}")
    private String port;

    @GetMapping(value = "/pay/get/info")
    private String getInfoByConsul(@Value("${win.info}") String winInfo)
    {
        return "winInfo: "+winInfo+"\t"+"port: "+port;
    }
}
